﻿var dataList = [], removeList = [], addList = [];
var vehicleid = GetQueryStr("vehicleId");
var isAdd = false;
var thistoday = "";

$(document).ready(function () {
    $('#calendar').fullCalendar({
        header: {
            left: 'myprevbutton today',
            center: 'title',
            right: 'mynextbutton'
        },
        monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        monthNamesShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        dayNames: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        today: ["今天"],
        firstDay: 1,
        buttonText: {
            today: '今天',
            month: '月',
            week: '周',
            day: '日',
            prev: '上一月',
            next: '下一月'
        },
        dayClick: function (date, jsEvent, view, resourceObj) {
            var selectData = date.format("YYYY/MM/DD");
            //判断 是否已经 添加的
            $(dataList).each(function (i, e) {
                if (e.start == selectData) { isAdd = true; return false; }
            });
            if (isAdd) { isAdd = false; return; }

            var obj = new Object();
            //判断是否被删除
            $(removeList).each(function (i, e) {
                if (e.start == selectData) {
                    obj.id = e.id;
                    isAdd = true;
                    return false;
                }
            });
            if (!isAdd) {obj.id = '999'; }

            obj.title = '已安排行程';
            obj.description = '不能再接业务了';
            obj.start = selectData;
            obj.color = 'red';

            //添加到数据库
            var opart = new jsonRow();
            opart.AddCell("id", obj.id);
            opart.AddCell("title", obj.title);
            opart.AddCell("description", obj.description);
            opart.AddCell("start", selectData);
            opart.AddCell("color", obj.color);
            opart.AddCell("vehicleid", vehicleid);

            opart.AddCell("status", 0);
            opart.AddCell("ACTION", "save");
            ajaxComm(opart);
            isAdd = false;
        },
        eventClick: function (calEvent, jsEvent, view) {
            if (calEvent.color != "red") {
                layer.msg('系统设置你没有更改权限', 1, 0);
                return;
            }
            var opart = new jsonRow();
            opart.AddCell("id", calEvent.id);
            opart.AddCell("title", '已安排行程');
            opart.AddCell("description", '不能再接业务了');
            opart.AddCell("start", "");
            opart.AddCell("color","red");
            opart.AddCell("vehicleid", vehicleid);

            opart.AddCell("status", 1);
            opart.AddCell("ACTION", "save");
            ajaxComm(opart);
        },
        customButtons: {
            myprevbutton: {
                text: '上一月',
                click: function (e) {
                    $(e).addClass("fc-state-disabled");
                    var isnext = nextToMonth(".fc-myprevbutton-button", ".fc-mynextbutton-button", 0, "prev");
                    if (isnext) { $('#calendar').fullCalendar('prev'); }
                }
            },
            mynextbutton: {
                text: '下一月',
                click: function () {
                    var isnext = nextToMonth(".fc-mynextbutton-button", ".fc-myprevbutton-button", 3, "next");
                    if (isnext) { $('#calendar').fullCalendar('next'); }
                }
            }
        },
        eventLimit: true,
        events: function (start, end, timezone, callback) {
            var sTime = start.format("YYYY/MM/DD");
            var eTime = end.format("YYYY/MM/DD");

            jQuery.loadHtml('/pgajax.axd?GET_SCHEDULE=ok&STIME=' + sTime + '&ETIME=' + eTime + '&VEHICLEID=' + vehicleid + '').done(function (_rets) {
                _rets = JSON.parse(_rets);
                if (_rets.records <= 0) { return; }
                var events = [];
                $(_rets.rows).each(function (i, e) {
                    if (e.VEHICLE_INFO_ID == vehicleid) {
                        var obj = new Object();
                        obj.id = e.VEHICLE_SCHEDULE_ID;
                        obj.title = e.SCHEDULE_TITLE;
                        obj.description = e.SCHEDULE_DESCRIPTION;
                        obj.start = e.START_TIME;
                        obj.end = e.END_TIME;
                        obj.color = e.SCHEDULE_COLOR;
                        obj.allDay = true;
                        events.push(obj);
                        dataList.push(obj);
                    }
                });
                callback(events);
               
            });
        },
        selectable: true,
        loading: function (bool) {
            layer.load('加载中，请稍后...', 1);
        }
    });

    //设置限制为三个月以内 的时间
    function nextToMonth(button1, button2, days, type) {
        //当前时间
        var moment = $('#calendar').fullCalendar('getDate').format("YYYY-MM-DD");
        if (thistoday == "") { thistoday = moment; }

        //当日起大约或等于3个月 禁用
        var fist = new Date(thistoday);
        var next = new Date(moment);

        var fistMonth = parseInt(fist.getMonth());
        var nextMonth = parseInt(next.getMonth());

        if ((nextMonth >= (fistMonth + days) && type == "next") || (type == "prev" && nextMonth <= fistMonth)) {
            //禁用
            $(button1).addClass("fc-state-disabled");
            $(button1).attr("disabled", "disabled");

            //启用
            if (!$(button2).hasClass("fc-state-disabled")) { return false; }
            $(button2).removeClass("fc-state-disabled");
            $(button2).removeAttr("disabled");
            return false;
        }
        else {
            //启用
            if (!$(button2).hasClass("fc-state-disabled")) { return true; }
            $(button2).removeClass("fc-state-disabled");
            $(button2).removeAttr("disabled");

            return true;
        }
    }

    //设置本月 已设置的时间
    function getToDayTime() {
        var vehicleId = GetQueryStr("vehicleId");


    }
});

//成功 保存
function success(data) {
    var obj = new Object();
    obj.id = data.id;
    obj.title = data.title;
    obj.description = data.description;
    obj.start = data.start;
    obj.color = data.color;
    obj.allDay = true;
    $('#calendar').fullCalendar('renderEvent', obj);  //核心的更新代码  
    $('#calendar').fullCalendar('unselect');

    //添加时候先查看 数据库是否已删除了
    $(dataList).each(function (i, e) {
        if (e.id == obj.id) { isAdd = true; return false; }
    });
    if (isAdd) { isAdd = false; return; }
    //移除
    $(removeList).each(function (i, e) {
        if (e.id == obj.id) { removeList.splice(i, 1); return false; }
    });
    //添加
    dataList.push(obj);


}
//修改
function usucccess(data) {
    //备注具体 点击发生
    $('#calendar').fullCalendar('removeEvents', data.id);
    $('#calendar').fullCalendar('unselect');
    //判断是否是 是数据库的 如果是添加到删除集合
    $(dataList).each(function (i, e) {
        if (e.id == data.id) {
            dataList.splice(i, 1);
            return false;
        }
    });

    $(removeList).each(function (i, e) {
        if (e.id == data.id) { isAdd = true; return false; }
    });
    if (!isAdd) {
        removeList.push({
            id: data.id,
            title: data.title,
            description: data.description,
            start: data.start,
            end: "",
            color: data.color
        });
    }

    isAdd = false;
}